package cn.wh.xwm.hibernate.dao;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import cn.wh.xwm.common.util.Constant;
import cn.wh.xwm.controller.action.SalesAction;
import cn.wh.xwm.controller.action.vo.CustomerView;
import cn.wh.xwm.hibernate.core.HibernateSessionFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;


/**
 * Data access object (DAO) for domain model
 * @author MyEclipse Persistence Tools
 */
public class BaseHibernateDAO implements IBaseHibernateDAO {
	private static final Log log = LogFactory.getLog(BaseHibernateDAO.class);
	public Session getSession() {
		return HibernateSessionFactory.getSession();
	}
	
	/**
	 * count(*) 方式的sql语句，用于条件查询时获取总记录数
	 * @param sql
	 * @return
	 */
	public String findTotalBysql(String sql){
		try {
			Query queryObject = getSession().createSQLQuery(sql);
			List list = queryObject.list();
			BigInteger count = (BigInteger) (list.get(0));
			return count.toString();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
		
	}
	
	public String findSum(String sql){
		try {
			Query queryObject = getSession().createSQLQuery(sql);
			List list = queryObject.list();
			BigDecimal count = (BigDecimal) (list.get(0));
			return count==null?"0":count.toString();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
		
	}
	
	
	/**
	 * 根据POJO名字查询记录 无条件
	 * @param pojoname
	 * @return
	 */
	public String findTotalCount(String pojoname){
		log.debug("finding all SalesLiquid instances");
		try {
			String queryString = "select count(*) from "+pojoname;
			Query queryObject = getSession().createQuery(queryString);
			
			List list = queryObject.list();
			Long count = (Long) (list.get(0));
			return count.toString();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	/**
	 * 分页查询 无条件
	 * @param page
	 * @param row
	 * @param pojoname
	 * @return
	 */
	public List findBypage(String page, String row,String pojoname) {
		if(row==null){
			row = Constant.ROWLIMIT;
		}
		if(page==null){
			page = Constant.FIRSTPAGE;
		}
		log.debug("finding all  by page and row in baseDAO");
		int ipage = Integer.valueOf(page);
		int irow = Integer.valueOf(row);
		int offset = (ipage - 1) * irow;
		try {
			String queryString = "from "+pojoname;
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(offset);
			queryObject.setMaxResults(irow);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	/**
	 * 分页查询 根据userId
	 * @param page
	 * @param row
	 * @param pojoname
	 * @return
	 */
	public List findBypage(String page, String row,String pojoname,String userId) {
		if(row==null){
			row = Constant.ROWLIMIT;
		}
		if(page==null){
			page = Constant.FIRSTPAGE;
		}
		log.debug("finding all  by page and row in baseDAO");
		int ipage = Integer.valueOf(page);
		int irow = Integer.valueOf(row);
		int offset = (ipage - 1) * irow;
		try {
			String queryString = "from "+pojoname+" where userId = '"+userId+"'";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(offset);
			queryObject.setMaxResults(irow);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
}